def validate_id(user_id):
    if len(user_id) > 8:
        return False
    for char in user_id:
        if not (char.isalnum() or char == '_'):
            return False
    return True

def validate_email(email):
    if '@' not in email or '.' not in email:
        return False
    if email.startswith('@') or email.endswith('@'):
        return False
    if email.startswith('.') or email.endswith('.'):
        return False
    return True


def validate_id_card(id_card):
    if len(id_card) != 18:
        return False
    for char in id_card[:-1]:
        if not char.isdigit():
            return False
    last_char = id_card[-1]
    if not (last_char.isdigit() or last_char.upper() == 'X'):
        return False
    return True


def validate_name(name):
    if len(name) > 4:
        return False
    for char in name:
        if not ('\u4e00' <= char <= '\u9fff'):  # 判断是否为中文字符
            return False
    return True


def get_gender(id_card):
    gender_digit = int(id_card[-2])
    return "男" if gender_digit % 2 == 1 else "女"

def main():
    print("欢迎使用用户注册小程序！")
    # 输入 ID
    while True:
        user_id = input("请输入 ID（长度不超过 8 位，只能由数字、字母、下划线组成）：")
        if validate_id(user_id):
            break
        print("ID 不合法，请重新输入！")
    # 输入 EMAIL
    while True:
        email = input("请输入 EMAIL 地址：")
        if validate_email(email):
            break
        print("EMAIL 地址不合法，请重新输入！")
    # 输入身份证号
    while True:
        id_card = input("请输入身份证号：")
        if validate_id_card(id_card):
            break
        print("身份证号不合法，请重新输入！")
    # 输入姓名
    while True:
        name = input("请输入姓名（只能中文且不超过 4 位）：")
        if validate_name(name):
            break
        print("姓名不合法，请重新输入！")

    # 获取性别
    gender = get_gender(id_card)
    # 输出用户信息
    print("\n用户注册成功！以下是您的信息：")
    print(f"ID: {user_id}")
    print(f"EMAIL: {email}")
    print(f"身份证号: {id_card}")
    print(f"姓名: {name}")
    print(f"性别: {gender}")

if __name__ == "__main__":
    main()